package com.wzh.house.conf;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.wzh.house.entity.NewHouseEntity;
import com.wzh.house.entity.RentHouseEntity;
import com.wzh.house.service.NewHouseService;
import com.wzh.house.service.RentHouseService;
import org.elasticsearch.action.index.IndexRequest;
import org.elasticsearch.action.index.IndexResponse;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.common.xcontent.XContentType;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.stereotype.Component;

import java.io.IOException;
import java.io.PipedReader;
import java.util.List;

/**
 * @author: 朱财艺
 * @date : 2022/03/24
 * @Description:
 */
@Component
public class MyApplication implements ApplicationRunner {

    @Autowired
    private NewHouseService newHouseService;

    @Autowired
    private RestHighLevelClient client;

    @Autowired
    private RentHouseService rentHouseService;

    @Override
    public void run(ApplicationArguments args) throws Exception {
//        List<NewHouseEntity> list = newHouseService.list(new QueryWrapper<NewHouseEntity>().eq("status",6));
//        for (NewHouseEntity newHouseEntity : list) {
//            IndexRequest request = new IndexRequest("buyhouse");
//            request.id(newHouseEntity.getHouseId()+"");
//            String jsonString = JSONObject.toJSONString(newHouseEntity);
//            request.source(jsonString, XContentType.JSON);
//            try {
//                IndexResponse indexResponse = client.index(request, RequestOptions.DEFAULT);
//            } catch (IOException e) {
//                e.printStackTrace();
//            }
//        }
        List<RentHouseEntity> list1 = rentHouseService.list(new QueryWrapper<RentHouseEntity>().eq("status",0));
        for (RentHouseEntity rentHouseEntity : list1) {
            IndexRequest request = new IndexRequest("renhouse");
            request.id(rentHouseEntity.getHouseId()+"");
            String jsonString = JSONObject.toJSONString(rentHouseEntity);
            request.source(jsonString, XContentType.JSON);
            try {
                IndexResponse indexResponse = client.index(request, RequestOptions.DEFAULT);
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}
